/**
 * Copyright (c) qc&Bull Co.,LTD.
 * All right reserved.
 */

package cn.zxd.lis.schema;

import cn.zxd.lis.db.LogTrackResultDB;
import cn.zxd.util.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * <p>ClassName: LogTrackResultSchema </p>
 * <p>Description: DB层 Schema 类文件 </p>
 * <p>Company: qc&Bull Co.,LTD </p>
 * @Database: IIEP_Data_Model
 * @author：anonymous
 * @CreateDate：2015-11-22
 */
public class LogTrackResultSchema implements Schema, Cloneable
{
    // @Field
    /** 日志号 */
    private String LogNo;
    /** 日志主题ID */
    private String SubjectID;
    /** 日志监控点ID */
    private String ItemID;
    /** 任务批次号码 */
    private double SerialNo;
    /** 控制点关键号码 */
    private String KeyNo;
    /** 控制点日志信息 */
    private String ItemDes;
    /** 日志辅助信息 */
    private String Remark;

    /** 日期处理 */
    private FDate fDate = new FDate();
    /** 日志产生日期 */
    private Date MakeDate;
    /** 日志产生时间 */
    private String MakeTime;

    /** 数据库表的字段个数 */
    public static final int FIELDNUM = 9;

    /** 主键 */
    private static String[] PK;

    /** 错误信息 */
    public CErrors mErrors;

    // @Constructor
    public LogTrackResultSchema()
    {
        mErrors = new CErrors();
        String[] pk = new String[1];
        pk[0] = "LogNo";
        PK = pk;
    }

    /**
     * Schema克隆
     * @return Object
     * @throws CloneNotSupportedException
     */
    public Object clone() throws CloneNotSupportedException
    {
        LogTrackResultSchema cloned = (LogTrackResultSchema)super.clone();
        cloned.fDate = (FDate) fDate.clone();
        cloned.mErrors = (CErrors) mErrors.clone();
        return cloned;
    }

    // @Method
    public String[] getPK()
    {
        return PK;
    }

    public String getLogNo()
    {
        return LogNo;
    }

    public void setLogNo(String aLogNo)
    {
        LogNo = StrTool.cTrim(aLogNo);
    }

    public String getSubjectID()
    {
        return SubjectID;
    }

    public void setSubjectID(String aSubjectID)
    {
        SubjectID = StrTool.cTrim(aSubjectID);
    }

    public String getItemID()
    {
        return ItemID;
    }

    public void setItemID(String aItemID)
    {
        ItemID = StrTool.cTrim(aItemID);
    }

    public double getSerialNo()
    {
        return SerialNo;
    }

    public void setSerialNo(double aSerialNo)
    {
        SerialNo = aSerialNo;
    }

    public void setSerialNo(String aSerialNo)
    {
        if (aSerialNo != null && !aSerialNo.equals(""))
        {
            Double tDouble = new Double(aSerialNo);
            SerialNo = tDouble.doubleValue();
        }
    }

    public String getKeyNo()
    {
        return KeyNo;
    }

    public void setKeyNo(String aKeyNo)
    {
        KeyNo = StrTool.cTrim(aKeyNo);
    }

    public String getItemDes()
    {
        return ItemDes;
    }

    public void setItemDes(String aItemDes)
    {
        ItemDes = StrTool.cTrim(aItemDes);
    }

    public String getRemark()
    {
        return Remark;
    }

    public void setRemark(String aRemark)
    {
        Remark = StrTool.cTrim(aRemark);
    }

    public String getMakeDate()
    {
        if (MakeDate == null)
            return null;
        else
            return fDate.getString(MakeDate);
    }

    public void setMakeDate(Date aMakeDate)
    {
        MakeDate = aMakeDate;
    }

    public void setMakeDate(String aMakeDate)
    {
        if (aMakeDate == null || aMakeDate.equals(""))
            MakeDate = null;
        else
            MakeDate = fDate.getDate(aMakeDate);
    }

    public String getMakeTime()
    {
        return MakeTime;
    }

    public void setMakeTime(String aMakeTime)
    {
        MakeTime = StrTool.cTrim(aMakeTime);
    }


    /**
     * 使用另外一个 LogTrackResultSchema 对象给 Schema 赋值
     * @param: cLogTrackResultSchema LogTrackResultSchema
     */
    public void setSchema(LogTrackResultSchema cLogTrackResultSchema)
    {
        this.LogNo = cLogTrackResultSchema.getLogNo();
        this.SubjectID = cLogTrackResultSchema.getSubjectID();
        this.ItemID = cLogTrackResultSchema.getItemID();
        this.SerialNo = cLogTrackResultSchema.getSerialNo();
        this.KeyNo = cLogTrackResultSchema.getKeyNo();
        this.ItemDes = cLogTrackResultSchema.getItemDes();
        this.Remark = cLogTrackResultSchema.getRemark();
        this.MakeDate = fDate.getDate(cLogTrackResultSchema.getMakeDate());
        this.MakeTime = cLogTrackResultSchema.getMakeTime();
    }

    /**
     * 使用 ResultSet 中的第 i 行给 Schema 赋值
     * @param: rs ResultSet
     * @param: i int
     * @return: boolean
     */
    public boolean setSchema(ResultSet rs, int i)
    {
        try
        {
            if (rs.getString(1) == null)
                this.LogNo = null;
            else
                this.LogNo = rs.getString(1);
            if (rs.getString(2) == null)
                this.SubjectID = null;
            else
                this.SubjectID = rs.getString(2);
            if (rs.getString(3) == null)
                this.ItemID = null;
            else
                this.ItemID = rs.getString(3);
            this.SerialNo = rs.getDouble(4);
            if (rs.getString(5) == null)
                this.KeyNo = null;
            else
                this.KeyNo = rs.getString(5);
            if (rs.getString(6) == null)
                this.ItemDes = null;
            else
                this.ItemDes = rs.getString(6);
            if (rs.getString(7) == null)
                this.Remark = null;
            else
                this.Remark = rs.getString(7);
            this.MakeDate = rs.getDate(8);
            if (rs.getString(9) == null)
                this.MakeTime = null;
            else
                this.MakeTime = rs.getString(9);
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LogTrackResultSchema";
            tError.functionName = "setSchema";
            tError.errorMessage = sqle.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    public LogTrackResultSchema getSchema()
    {
        LogTrackResultSchema aLogTrackResultSchema = new LogTrackResultSchema();
        aLogTrackResultSchema.setSchema(this);
        return aLogTrackResultSchema;
    }

    public LogTrackResultDB getDB()
    {
        LogTrackResultDB aDBOper = new LogTrackResultDB();
        aDBOper.setSchema(this);
        return aDBOper;
    }

    /**
     * 数据打包，按 XML 格式打包
     * @return: String 返回打包后字符串
     */
    public String encode()
    {
        StringBuffer strReturn = new StringBuffer(256);
        strReturn.append(StrTool.cTrim(LogNo));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(SubjectID));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(ItemID));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(ChgData.chgData(SerialNo));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(KeyNo));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(ItemDes));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(Remark));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(fDate.getString(MakeDate)));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(MakeTime));
        return strReturn.toString();
    }

    /**
     * 数据解包
     * @param: strMessage String 包含一条纪录数据的字符串
     * @return: boolean
     */
    public boolean decode(String strMessage)
    {
        try
        {
            LogNo = StrTool.getStr(strMessage, 1, SysConst.PACKAGESPILTER);
            SubjectID = StrTool.getStr(strMessage, 2, SysConst.PACKAGESPILTER);
            ItemID = StrTool.getStr(strMessage, 3, SysConst.PACKAGESPILTER);
            SerialNo = new Double(ChgData.chgNumericStr(StrTool.getStr(strMessage, 4, SysConst.PACKAGESPILTER))).doubleValue();
            KeyNo = StrTool.getStr(strMessage, 5, SysConst.PACKAGESPILTER);
            ItemDes = StrTool.getStr(strMessage, 6, SysConst.PACKAGESPILTER);
            Remark = StrTool.getStr(strMessage, 7, SysConst.PACKAGESPILTER);
            MakeDate = fDate.getDate(StrTool.getStr(strMessage, 8, SysConst.PACKAGESPILTER));
            MakeTime = StrTool.getStr(strMessage, 9, SysConst.PACKAGESPILTER);
        }
        catch (NumberFormatException ex)
        {
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LogTrackResultSchema";
            tError.functionName = "decode";
            tError.errorMessage = ex.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    /**
     * 取得对应传入参数的String形式的字段值
     * @param: FCode String 希望取得的字段名
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(String FCode)
    {
        String strReturn = "";
        if (FCode.equalsIgnoreCase("LogNo"))
        {
            strReturn = StrTool.cTrim(LogNo);
        }
        if (FCode.equalsIgnoreCase("SubjectID"))
        {
            strReturn = StrTool.cTrim(SubjectID);
        }
        if (FCode.equalsIgnoreCase("ItemID"))
        {
            strReturn = StrTool.cTrim(ItemID);
        }
        if (FCode.equalsIgnoreCase("SerialNo"))
        {
            strReturn = String.valueOf(SerialNo);
        }
        if (FCode.equalsIgnoreCase("KeyNo"))
        {
            strReturn = StrTool.cTrim(KeyNo);
        }
        if (FCode.equalsIgnoreCase("ItemDes"))
        {
            strReturn = StrTool.cTrim(ItemDes);
        }
        if (FCode.equalsIgnoreCase("Remark"))
        {
            strReturn = StrTool.cTrim(Remark);
        }
        if (FCode.equalsIgnoreCase("MakeDate"))
        {
            strReturn = StrTool.cTrim(this.getMakeDate());
        }
        if (FCode.equalsIgnoreCase("MakeTime"))
        {
            strReturn = StrTool.cTrim(MakeTime);
        }
        if (strReturn.equals(""))
        {
            strReturn = "null";
        }
        return strReturn;
    }

    /**
     * 取得Schema中指定索引值所对应的字段值
     * @param: nFieldIndex int 指定的字段索引值
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(int nFieldIndex)
    {
        String strFieldValue = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldValue = StrTool.cTrim(LogNo);
                break;
            case 1:
                strFieldValue = StrTool.cTrim(SubjectID);
                break;
            case 2:
                strFieldValue = StrTool.cTrim(ItemID);
                break;
            case 3:
                strFieldValue = String.valueOf(SerialNo);
                break;
            case 4:
                strFieldValue = StrTool.cTrim(KeyNo);
                break;
            case 5:
                strFieldValue = StrTool.cTrim(ItemDes);
                break;
            case 6:
                strFieldValue = StrTool.cTrim(Remark);
                break;
            case 7:
                strFieldValue = StrTool.cTrim(this.getMakeDate());
                break;
            case 8:
                strFieldValue = StrTool.cTrim(MakeTime);
                break;
            default:
                strFieldValue = "";
        }
        if (strFieldValue.equals(""))
        {
            strFieldValue = "null";
        }
        return strFieldValue;
    }

    /**
     * 设置对应传入参数的String形式的字段值
     * @param: FCode String 需要赋值的对象
     * @param: FValue String 要赋的值
     * @return: boolean
     */
    public boolean setV(String FCode, String FValue)
    {
        if (StrTool.cTrim(FCode).equals(""))
            return false;

        if (FCode.equalsIgnoreCase("LogNo"))
        {
            if (FValue != null)
            {
                LogNo = FValue.trim();
            }
            else
                LogNo = null;
        }
        if (FCode.equalsIgnoreCase("SubjectID"))
        {
            if (FValue != null)
            {
                SubjectID = FValue.trim();
            }
            else
                SubjectID = null;
        }
        if (FCode.equalsIgnoreCase("ItemID"))
        {
            if (FValue != null)
            {
                ItemID = FValue.trim();
            }
            else
                ItemID = null;
        }
        if (FCode.equalsIgnoreCase("SerialNo"))
        {
            if (FValue != null && !FValue.equals(""))
            {
                Double tDouble = new Double(FValue);
                double d = tDouble.doubleValue();
                SerialNo = d;
            }
        }
        if (FCode.equalsIgnoreCase("KeyNo"))
        {
            if (FValue != null)
            {
                KeyNo = FValue.trim();
            }
            else
                KeyNo = null;
        }
        if (FCode.equalsIgnoreCase("ItemDes"))
        {
            if (FValue != null)
            {
                ItemDes = FValue.trim();
            }
            else
                ItemDes = null;
        }
        if (FCode.equalsIgnoreCase("Remark"))
        {
            if (FValue != null)
            {
                Remark = FValue.trim();
            }
            else
                Remark = null;
        }
        if (FCode.equalsIgnoreCase("MakeDate"))
        {
            if (FValue != null)
            {
                MakeDate = fDate.getDate(FValue);
            }
            else
                MakeDate = null;
        }
        if (FCode.equalsIgnoreCase("MakeTime"))
        {
            if (FValue != null)
            {
                MakeTime = FValue.trim();
            }
            else
                MakeTime = null;
        }
        return true;
    }

    public boolean equals(Object otherObject)
    {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (getClass() != otherObject.getClass())
            return false;
        LogTrackResultSchema other = (LogTrackResultSchema) otherObject;
        return
                (LogNo == null ? other.getLogNo() == null : LogNo.equals(other.getLogNo()))
                        && (SubjectID == null ? other.getSubjectID() == null : SubjectID.equals(other.getSubjectID()))
                        && (ItemID == null ? other.getItemID() == null : ItemID.equals(other.getItemID()))
                        && SerialNo == other.getSerialNo()
                        && (KeyNo == null ? other.getKeyNo() == null : KeyNo.equals(other.getKeyNo()))
                        && (ItemDes == null ? other.getItemDes() == null : ItemDes.equals(other.getItemDes()))
                        && (Remark == null ? other.getRemark() == null : Remark.equals(other.getRemark()))
                        && (MakeDate == null ? other.getMakeDate() == null : fDate.getString(MakeDate).equals(other.getMakeDate()))
                        && (MakeTime == null ? other.getMakeTime() == null : MakeTime.equals(other.getMakeTime()));
    }

    /**
     * 取得Schema拥有字段的数量
     * @return: int
     */
    public int getFieldCount()
    {
        return FIELDNUM;
    }

    /**
     * 取得Schema中指定字段名所对应的索引值
     * 如果没有对应的字段，返回-1
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldIndex(String strFieldName)
    {
        if (strFieldName.equals("LogNo"))
        {
            return 0;
        }
        if (strFieldName.equals("SubjectID"))
        {
            return 1;
        }
        if (strFieldName.equals("ItemID"))
        {
            return 2;
        }
        if (strFieldName.equals("SerialNo"))
        {
            return 3;
        }
        if (strFieldName.equals("KeyNo"))
        {
            return 4;
        }
        if (strFieldName.equals("ItemDes"))
        {
            return 5;
        }
        if (strFieldName.equals("Remark"))
        {
            return 6;
        }
        if (strFieldName.equals("MakeDate"))
        {
            return 7;
        }
        if (strFieldName.equals("MakeTime"))
        {
            return 8;
        }
        return -1;
    }

    /**
     * 取得Schema中指定索引值所对应的字段名
     * 如果没有对应的字段，返回""
     * @param: nFieldIndex int
     * @return: String
     */
    public String getFieldName(int nFieldIndex)
    {
        String strFieldName = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldName = "LogNo";
                break;
            case 1:
                strFieldName = "SubjectID";
                break;
            case 2:
                strFieldName = "ItemID";
                break;
            case 3:
                strFieldName = "SerialNo";
                break;
            case 4:
                strFieldName = "KeyNo";
                break;
            case 5:
                strFieldName = "ItemDes";
                break;
            case 6:
                strFieldName = "Remark";
                break;
            case 7:
                strFieldName = "MakeDate";
                break;
            case 8:
                strFieldName = "MakeTime";
                break;
            default:
                strFieldName = "";
        }
        return strFieldName;
    }

    /**
     * 取得Schema中指定字段名所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldType(String strFieldName)
    {
        if (strFieldName.equals("LogNo"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("SubjectID"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("ItemID"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("SerialNo"))
        {
            return Schema.TYPE_DOUBLE;
        }
        if (strFieldName.equals("KeyNo"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("ItemDes"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("Remark"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("MakeDate"))
        {
            return Schema.TYPE_DATE;
        }
        if (strFieldName.equals("MakeTime"))
        {
            return Schema.TYPE_STRING;
        }
        return Schema.TYPE_NOFOUND;
    }

    /**
     * 取得Schema中指定索引值所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: nFieldIndex int
     * @return: int
     */
    public int getFieldType(int nFieldIndex)
    {
        int nFieldType = Schema.TYPE_NOFOUND;
        switch (nFieldIndex)
        {
            case 0:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 1:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 2:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 3:
                nFieldType = Schema.TYPE_DOUBLE;
                break;
            case 4:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 5:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 6:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 7:
                nFieldType = Schema.TYPE_DATE;
                break;
            case 8:
                nFieldType = Schema.TYPE_STRING;
                break;
            default:
                nFieldType = Schema.TYPE_NOFOUND;
        }
        return nFieldType;
    }
}
